package com.ulook.planetmanager.ui.home;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.ulook.planetmanager.R;
import com.ulook.planetmanager.base.BaseFragment;
import com.ulook.planetmanager.model.TUser;
import com.ulook.planetmanager.util.StringUtil;
import com.ulook.planetmanager.widget.FriendView;

import java.util.List;

public class HomeFragment extends BaseFragment {

    private HomeViewModel viewModel;
    private View root;
    private LinearLayout userLayout;
    private EditText userSearch;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        viewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        root = inflater.inflate(R.layout.fragment_home, container, false);
        viewModel.getToastText().observe(getActivity(), s -> {
            Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
        });
        viewModel.getUsers().observe(getActivity(), tUsers -> {
            displayUsers(tUsers);
        });

        QMUITopBarLayout topBar = root.findViewById(R.id.user_bar);
        topBar.setTitle(R.string.title_home);

        return root;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        String s = userSearch.getText().toString();
        if(!StringUtil.isEmpty(s)){
            viewModel.searchUser(s);
        }
    }

    private void initData(){
        userLayout = root.findViewById(R.id.user_layout_main);
        userSearch = root.findViewById(R.id.user_search);
        Button bt = root.findViewById(R.id.user_search_bt);
        bt.setOnClickListener(view -> {
            String s = userSearch.getText().toString();
            if(!StringUtil.isEmpty(s)){
                userLayout.removeAllViews();
                viewModel.searchUser(s);
            }
        });
    }

    /**
     * 展示搜索结果
     */
    private void displayUsers(List<TUser> users){
        if(users.size() == 0){
            Toast.makeText(getActivity(), "未查询到结果", Toast.LENGTH_SHORT).show();
            return;
        }
        for(int i = 0; i < users.size(); i++){
            FriendView friendView = new FriendView(getActivity());
            friendView.setWidgetData(users.get(i), 0);
            userLayout.addView(friendView, 0);
        }
    }
}